<template>
	<div class="sys-position-container">
		<el-dialog v-model="state.isShowDialog" draggable width="600px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.posInfo.belongOrg')" >
							<el-cascader
								:options="props.orgData"
								:props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'orgName' }"
								:placeholder="$t('message.posInfo.belongOrg')"
								
								class="w100"
								v-model="state.ruleForm.orgId"
								:rules="[{ required: true, message: t('message.posAlter.belongOrgEmpty'), trigger: 'blur' }]"
							>
								<template #default="{ node, data }">
									<span>{{ data.orgName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.posInfo.posName')" prop="name" :rules="[{ required: true, message: t('message.posAlter.posNameEmpty'), trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" :placeholder="$t('message.posInfo.posName')"  />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.posInfo.posCode')" prop="code" :rules="[{ required: true, message: t('message.posAlter.posCodeEmpty'), trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.code" :placeholder="$t('message.posInfo.posCode')"  />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.posInfo.posType')" prop="category" >
							<el-select v-model="state.ruleForm.category" :placeholder="$t('message.posInfo.posType')" :rules="[{ required: true, message: t('message.posAlter.posTypeEmpty'), trigger: 'blur' }]">
								<el-option v-for="d in props.dictData" :key="d.id" :label="d.name" :value="d.value" />,
						</el-select>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sortCode" placeholder="排序" class="w100" />
						</el-form-item>
					</el-col>  -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.posInfo.remark')">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注内容"  type="textarea" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">{{$t('message.posOperation.cancel')}}</el-button>
					<el-button type="primary" @click="submit" size="default">{{$t('message.posOperation.submit')}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditpos">
import { reactive, ref } from 'vue';
import { feature } from '/@/utils/axios-utils';
import { SysPosition, PositionUpdateInput, SysDictData,SysOrg } from '/@/api-services/models/system';
import { ElMessage } from "element-plus";
import { useI18n } from 'vue-i18n';
import positionApi from '/@/api-services/apis/system/positionApi';
// 定义变量内容
const { t } = useI18n();
const props = defineProps({
	title: String,
	posData: Array<SysPosition>,
	dictData: Array<SysDictData>,
	orgData: Array<SysOrg>
});

const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as PositionUpdateInput,
});

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};
// 与父组件的交互逻辑
const emits = defineEmits(['refresh']);
// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		var err:Error|null
		if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
			[err]=await feature(positionApi.update(state.ruleForm));
		} else {
			[err]=await feature(positionApi.add(state.ruleForm));
		}
		if(err){
			ElMessage.error(err.message)
		}
		else{
			emits('refresh');
			state.isShowDialog = false;
		}		
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
